<template>
  <div>
    <el-button @click="handleUnSelected">取消选中</el-button>
    <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="id" default-expand-all @row-click="rowClick" :row-class-name="rowClassName">
      <el-table-column prop="date" label="Date" sortable />
      <el-table-column prop="name" label="Name" sortable />
      <el-table-column prop="address" label="Address" sortable />
    </el-table>
    <Test>
      <div>default</div>
      <template #header="{ data: { data } }">
        {{ data.a }}
      </template>
      <template #footer="{ data: { data } }">
        {{ data }}
      </template>
    </Test>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import Test from './Test.vue'

interface User {
  id: number
  date: string
  name: string
  address: string
  hasChildren?: boolean
  children?: User[]
}


const tableData: User[] = [
  {
    id: 1,
    date: '2016-05-02',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 2,
    date: '2016-05-04',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 3,
    date: '2016-05-01',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles',
    children: [
      {
        id: 31,
        date: '2016-05-01',
        name: 'wangxiaohu',
        address: 'No. 189, Grove St, Los Angeles',
      },
      {
        id: 32,
        date: '2016-05-01',
        name: 'wangxiaohu',
        address: 'No. 189, Grove St, Los Angeles',
      },
    ],
  },
  {
    id: 4,
    date: '2016-05-03',
    name: 'wangxiaohu',
    address: 'No. 189, Grove St, Los Angeles',
  },
]
const selectedRowId = ref('')
/**
 * @description: 行点击事件
 * @param {*} row
 */
const rowClick = (row: any) => {
  selectedRowId.value = row.id
}
/**
 * @description: 行样式
 * @param {*} rowData
 * @return {*}
 */
const rowClassName = (rowData: any) => {
  if (rowData.row.id === selectedRowId.value) {
    return 'row-selected'
  } else {
    return ''
  }
}
// 取消选中
const handleUnSelected = () => {
  selectedRowId.value = ''
}

</script>

<style lang="scss" scoped>
:deep(.row-selected) {
  background-color: skyblue;
}
:deep(.el-table--enable-row-hover .el-table__body tr.row-selected:hover>td.el-table__cell) {
  background-color: unset;
}
</style>
